import requests
import itchat
from datetime import datetime
import threading
import time

class NotificationManager:
    def __init__(self):
        self.sms_api_key = "YOUR_SMS_API_KEY"  # 需要替换为实际的短信API密钥
        self.wechat_logged_in = False
        self.enable_wechat = False  # 添加开关控制
        
    def start_wechat(self):
        if not self.enable_wechat:  # 只在需要时才启动微信
            return
        def login():
            itchat.auto_login(hotReload=True)
            self.wechat_logged_in = True
            itchat.run()
            
        threading.Thread(target=login, daemon=True).start()
        
    def send_wechat(self, user_id, message):
        if not self.wechat_logged_in:
            return False
        try:
            itchat.send(message, toUserName=user_id)
            return True
        except:
            return False
            
    def send_sms(self, phone_number, message):
        # 这里使用示例短信API，需要替换为实际的短信服务
        try:
            response = requests.post(
                'https://api.example.com/sms/send',
                json={
                    'apiKey': self.sms_api_key,
                    'phone': phone_number,
                    'message': message
                }
            )
            return response.status_code == 200
        except:
            return False
            
    def send_period_reminder(self, user, days_until):
        message = f"温馨提醒：距离您的下一次经期还有{days_until}天"
        
        if user.wechat_id:
            self.send_wechat(user.wechat_id, message)
            
        if user.phone_number:
            self.send_sms(user.phone_number, message) 